package com.itheima.controller;

import com.itheima.entity.AliOssProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {
    @Autowired
    private AliOssProperties aliOssProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliyunOSSUtils.upload(aliOssProperties.getEndpoint(), aliOssProperties.getBucket(), file.getBytes(), extention);
        return Result.success(url);
    }
}
